package com.cjm.algorithm.dao;

public class Ti200 {
    public static void main(String[] args) {


    }
    public int numIslands(char[][] grid) {
        res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    res++;
                }
                if (grid[i][j] == '2' || grid[i][j] == '0') {
                    continue;
                }
                method200(grid, i, j);
            }
        }
        return res;
    }

    int res = 0;

    private void method200(char[][] grid, int x, int y) {
        // 1, 2, 0
        if (!isValid(grid, x, y)) {
            return;
        }
        if (grid[x][y] == '0' || grid[x][y] == '2') {
            return;
        }

        if (grid[x][y] == '1') {
            grid[x][y] = '2';
        }
        method200(grid, x - 1, y);
        method200(grid, x + 1, y);
        method200(grid, x, y - 1);
        method200(grid, x, y + 1);
    }


    public void xx(char[][] grid, int x, int y) {
        // 如果已经超出边界了
        if (!isValid(grid, x, y)) {
            return;
        }
        // 上、下、左、右
        xx(grid, x - 1, y);// 上
        xx(grid, x + 1, y);// 下
        xx(grid, x, y - 1);// 左
        xx(grid, x, y + 1);// 右
    }

    private boolean isValid(char[][] grid, int x, int y) {
        return x >= 0 && y >= 0 && x < grid.length && y < grid[0].length;
    }
}
